package com.friend.controller;

import com.intelligt.modbus.jlibmodbus.Modbus;
import com.intelligt.modbus.jlibmodbus.exception.ModbusIOException;
import com.intelligt.modbus.jlibmodbus.exception.ModbusNumberException;
import com.intelligt.modbus.jlibmodbus.exception.ModbusProtocolException;
import com.intelligt.modbus.jlibmodbus.master.ModbusMaster;
import com.intelligt.modbus.jlibmodbus.master.ModbusMasterFactory;
import com.intelligt.modbus.jlibmodbus.tcp.TcpParameters;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.net.InetAddress;

/**
 * @author zyc
 * @since 2023-03-22 14:51
 */
@RestController
public class ModbusController {
    @GetMapping("/modbus")
    public void get() {
        try {
            // 设置主机TCP参数
            TcpParameters tcpParameters = new TcpParameters();

            // 设置TCP的ip地址-本地地址
            InetAddress adress = InetAddress.getByName("192.168.72.188");
            // tcpParameters.setHost(InetAddress.getLocalHost());
            tcpParameters.setHost(adress);

            // TCP设置长连接
            tcpParameters.setKeepAlive(true);
            // TCP设置端口，这里设置是默认端口502
            tcpParameters.setPort(10051);

            // 创建一个主机
            ModbusMaster master = ModbusMasterFactory.createModbusMasterTCP(tcpParameters);
            Modbus.setAutoIncrementTransactionId(true);

            int slaveId = 1;//从机地址
            int offset = 0;//寄存器读取开始地址
            int quantity = 10;//读取的寄存器数量

            System.out.println("准备执行开启连接");
            try {
                if (!master.isConnected()) {
                    master.connect();// 开启连接
                }
                System.out.println("读取对应从机的数据");
                //读取对应从机的数据，这里演示的是功能码03
                //功能码01 readCoils()
                //功能码02 readDiscreteInputs()
                //功能码03 readHoldingRegisters()
                //功能码04 readInputRegisters()
                int[] registerValues = master.readHoldingRegisters(slaveId, offset, quantity);
                System.out.println("成功！");
                // 控制台输出
                for (int value : registerValues) {
                    System.out.println("Address: " + offset++ + ", Value: " + value);
                }
            } catch (ModbusProtocolException e) {
                e.printStackTrace();
            } catch (ModbusNumberException e) {
                e.printStackTrace();
            } catch (ModbusIOException e) {
                e.printStackTrace();
            } finally {
                try {
                    master.disconnect();
                } catch (ModbusIOException e) {
                    e.printStackTrace();
                }
            }
        } catch (RuntimeException e) {
            throw e;
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
//https://blog.csdn.net/weixin_44727617/article/details/125655269
    public static void main(String[] args) {
        try {
            // 设置主机TCP参数
            TcpParameters tcpParameters = new TcpParameters();

            // 设置TCP的ip地址-本地地址
            InetAddress adress = InetAddress.getByName("192.168.72.188");
            // tcpParameters.setHost(InetAddress.getLocalHost());
            tcpParameters.setHost(adress);

            // TCP设置长连接
            tcpParameters.setKeepAlive(true);
            // TCP设置端口，这里设置是默认端口502
            tcpParameters.setPort(10051);

            // 创建一个主机
            ModbusMaster master = ModbusMasterFactory.createModbusMasterTCP(tcpParameters);
            Modbus.setAutoIncrementTransactionId(true);

            int slaveId = 1;//从机地址
            int offset = 307;//寄存器读取开始地址
            int quantity = 1;//读取的寄存器数量

            System.out.println("准备执行开启连接");
            try {
                if (!master.isConnected()) {
                    master.connect();// 开启连接
                }
                System.out.println("读取对应从机的数据");
                //读取对应从机的数据，这里演示的是功能码03
                //功能码01 readCoils()
                //功能码02 readDiscreteInputs()
                //功能码03 readHoldingRegisters()
                //功能码04 readInputRegisters()
//                int[] registerValues = master.readHoldingRegisters(slaveId, offset, quantity);
                int[] registerValues = master.readInputRegisters(slaveId, offset, quantity);
                System.out.println("成功！");
                // 控制台输出
                for (int value : registerValues) {
                    System.out.println("Address: " + offset++ + ", Value: " + value);
                }
            } catch (ModbusProtocolException e) {
                e.printStackTrace();
            } catch (ModbusNumberException e) {
                e.printStackTrace();
            } catch (ModbusIOException e) {
                e.printStackTrace();
            } finally {
                try {
                    master.disconnect();
                } catch (ModbusIOException e) {
                    e.printStackTrace();
                }
            }
        } catch (RuntimeException e) {
            throw e;
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
